
package vek.controllers.SearchAndList;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import vek.GUI.MapView.ScrollableView;
import vek.GUI.SearchAndListView.RightPanelView;

/**
 * Controls the user input from the right panel.
 * This class is also responsible for showing the POIs as strings in the RightPanelView.
 * @author Christian, Michal og Jakob (Hold 1, gruppe 2)
 */
public class RightPanelViewController implements ActionListener{
    
    private ArrayList<String> noPOIsFound =  new ArrayList<String>();
    private RightPanelView rightPanelView;
    private ScrollableView scrollableView;
    private ArrayList<Point> currentListOfLocations;
    
    /**
     * 
     * @param rpv The RightPanelView which is the right panel that shows the POIs that have been searched for
     */
    
    public RightPanelViewController(RightPanelView rpv)
    {
        rightPanelView = rpv;
        
        noPOIsFound.add("Ingen serværdigheder fundet");
        
    }
    
    /**
     * Sets the ScrollView. The scroll view is the main
     * @param sv
     */
    
    public void setScrollView(ScrollableView sv)
    {
        scrollableView = sv;
    }
    
    /**
     * This method runs each time user input occurs.
     * When the user clicks one of the POI buttons, the screen snaps (centers) on the POI.
     * @param e ActionEvent created by the user input
     */

    public void actionPerformed(ActionEvent e) {
        
        JButton button = (JButton) e.getSource();
        if(button.getName().startsWith("hid"))
        {
            rightPanelView.toggleVisible();
        }
        else
        {
            
            int buttonNumber = Integer.valueOf(button.getName().substring( (button.getName().indexOf(":") + 1), button.getName().length()));
            scrollableView.zapTo(currentListOfLocations.get(buttonNumber));   
        }
        
        
    }
    
    /**
     * Shows the POIs in the right panel.
     * @param pois
     */
    
    public void loadListOfPOIs(ArrayList<ArrayList> pois)
    {
        assert rightPanelView != null : "RightPanelViewController not set";
        assert pois.size() == 2 : "Right panel controller, pois wrong size";
        
        currentListOfLocations = pois.get(1);
        
        if(pois.get(0).size() == 0)
        {
            rightPanelView.showPOIs(noPOIsFound);
            //rightPanelView.setVisible(false);
            rightPanelView.revalidate();
            rightPanelView.repaint();
            return;
        }
        
        else if(rightPanelView.isVisible() == false)
        {
            rightPanelView.setVisible(true);
        }
        
        rightPanelView.showPOIs(pois.get(0));
        rightPanelView.revalidate();
        rightPanelView.repaint();
    }
    
    /**
     * removes all POIs from the right panel,
     */
    
    public void clearListOfPOIs()
    {
        rightPanelView.clearAllPOIs();
    }
    

    

}
